package com.lkd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.SkuClassDto;
import com.lkd.entity.SkuClass;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.SkuClassService;
import com.lkd.vo.SkuClassVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @className: ShopController
 * @description:
 * @author: InnovatorX
 * @date: 2024/5/26 21:15
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/vm-service/skuClass")
@Slf4j
public class SkuClassController extends Result{
    @Autowired
    private SkuClassService skuClassService;

    /**
     * 新增分类
     *
     * @param skuClassDto
     * @return
     */
    @PostMapping
    public Result saveShop(@RequestBody SkuClassDto skuClassDto) {
        log.info("保存skuClass:{}", skuClassDto);
        SkuClass skuClass = BeanUtil.toBean(skuClassDto, SkuClass.class);
        skuClassService.saveShop(skuClass);
        return success(true);
    }

    /**
     * 分类搜索
     *
     * @param className
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result<SkuClassVo> search (
            @RequestParam(name = "className",required = false) String className,
            @RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageBean<SkuClassVo> skuClassVo = skuClassService.search(className,pageIndex, pageSize);

        return success(skuClassVo);
    }

    /**
     * 获取分类基本信息
     * @param classId
     * @return
     */
    @GetMapping("/{classId}")
    public Result<SkuClassVo> getById(@PathVariable("classId") Integer classId) {
        SkuClassVo dto = skuClassService.getById(classId);
        return success(dto);
    }

    /**
     * 修改分类
     * @param skuClassDto
     * @return
     */
    @PutMapping
    public Result update(@RequestBody SkuClassDto skuClassDto) {
        SkuClass skuClass = BeanUtil.toBean(skuClassDto, SkuClass.class);
        skuClassService.update(skuClass);
        return success(true);
    }

    /**
     * 删除分类
     * @param classId
     * @return
     */
    @DeleteMapping("/{classId}")
    public Result delete(@PathVariable("classId") String classId) {
        Integer parseInt = Integer.parseInt(classId);
        skuClassService.delete(parseInt);
        return success(true);
    }

}
